package com.example.easyscript.utils;

import com.example.easyscript.dto.BaseMatchResult;
import com.example.easyscript.utils.recognization.MatchTemplate;
import com.example.easyscript.utils.recognization.RecognizationSURF;
import com.example.easyscript.utils.recognization.RecognizationSURFPro;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageUtils {

    final static int PASSPOINTCOUNT = 5;

    final static float DEFAULTCONFIDENCE = 0.8f;

    /**
     * 判断模板是否在原图之中
     */
    public static Boolean judgeTemplateIsInSource(String templatePath, String sourcePath) {
        return judgeTemplateIsInSource(templatePath, sourcePath, DEFAULTCONFIDENCE);
    }

    /**
     * 判断模板是否在原图之中
     */
    public static Boolean judgeTemplateIsInSource(String templatePath, String sourcePath, float confidence) {
        return RecognizationSURFPro.judgeTemplateIsInSource(templatePath, sourcePath, confidence, PASSPOINTCOUNT);
    }

    /**
     * 确定模板的点击点
     */
    public static BaseMatchResult getTemplateCenterPoint(String templatePath, String sourcePath) {
        return MatchTemplate.getTemplateCenterPoint(templatePath, sourcePath);
    }

    /**
     * 保存图片到指定地址
     */
    public static String saveImage(MultipartFile file, String path) throws Exception {
        BufferedImage image = null;
        File outPut = null;
        outPut = new File(path + "/" + file.getOriginalFilename());
        image = ImageIO.read(file.getInputStream());
        ImageIO.write(image, "png", outPut);
        System.out.println("图片保存成功");
        return path + "/" + file.getOriginalFilename();
    }

}